package com.codejiwei.core.graphx

import org.apache.spark._
import org.apache.spark.graphx.PartitionStrategy
import org.apache.spark.graphx.util.GraphGenerators

object GraphX_PartitionBy {
  def main(args: Array[String]): Unit = {
    val sc = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("GraphXPartitionBy"))
    val graph = GraphGenerators.logNormalGraph(sc, numVertices = 10, numEParts = 5, mu = 2.0, sigma = 1.5)

    //TODO 用于重新分区图的顶点和边，根据指定的分区策略。重新分区可以用于优化图的性能和并行执行，以便更有效地执行一些图算法和操作

    // 定义一个基于属性的分区策略，例如按照顶点属性的哈希值进行分区
    val partitionStrategy = PartitionStrategy.EdgePartition2D

    // 根据指定的分区策略重新分区图
    val partitionedGraph = graph.partitionBy(partitionStrategy)

    // 对重新分区后的图执行一些操作
    val numPartitions = partitionedGraph.edges.partitions.length

    println(s"the graph num partitions: $numPartitions")
  }
}
